package com.jiang.booksystem.controller;

import com.jiang.booksystem.core.ApiResponse;
import com.jiang.booksystem.dao.entity.Book;
import com.jiang.booksystem.dao.entity.dos.BookAggre;
import com.jiang.booksystem.manager.BookManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BookController {

    @Autowired
    BookManager bookManager;

    @GetMapping("/book/add")
    public ApiResponse<Void> add(
            @RequestParam(value = "name", required = true) String name,
            String author,
            Double price
            ) throws Exception {

        Book a = new Book();

        a.setName(name);
        a.setAuthor(author);
        a.setPrice(price);


        bookManager.add(a);
        return new ApiResponse<>(1, "添加成功", null);
    }

    //修改库存
    @GetMapping("/book/updateStock")
    public ApiResponse<Void> updateStock(String name,int stock){
        bookManager.updateStock(name,stock);
        return new ApiResponse<>(1,"修改库存成功",null);

    }

    @GetMapping("/book/search")
    public ApiResponse< List<BookAggre>> search() {
        List<BookAggre> data = bookManager.search(null);
        return new ApiResponse<>(1, null, data);
    }

    @GetMapping("/book/update")
    public ApiResponse<Void> updateBase(String name, double price) {
        bookManager.updateBase(name, price);
        return new ApiResponse<>(1, "修改成功", null);
    }

    @GetMapping("/book/delete")
    public ApiResponse<Void>  delete(String name) {
        bookManager.delete(name);
        return new ApiResponse<>(1, "删除成功", null);
    }



}
